<template>
	<view class="list">
		<view class="user-group" v-for="(userList, index) in userGroupList" :key="index">
			<view class="user" 
			v-for="(user, idx) in userList.users" 
			:key="idx" 
			:url="'/user/userinfo?id='+user.name" 
			@tap="gotoUserInfo(user.name)">
				<image class="avatar" :src="user.avatar" mode="aspectFill"></image>
				<view class="name">{{user.tag}}{{user.name}}</view>
			</view>
		</view>
		<view class="grouo-indexs">
			<view class="gindex" 
			v-for="(item, index)  in groupTags"  
			:key="index" 
			:class=" currentTag == item ? 'chosed' : '' " 
			@tap="gotoTag(item)">
				{{item}}
			</view>
		</view>
	</view>
</template>

<script>
	import userList from '@/common/usersLsit.js'
	export default {
		data(){
			return {
				currentTag: 'A',
			}
		},
		onPageScroll(e){
			for( let ug in this.userGroupList){
				if(this.userGroupList[ug].top <= e.scrollTop){
					this.currentTag = ug
				}
			}
		},
		computed:{
			userGroupList(){
				// 第一步：咱们对原始数据进行排序
				let userSort = userList.sort((a,b) => {
					return a.tag.localeCompare(b.tag)
				})
				console.log(userSort)
				// 第二步：实现咱们根据 tag 进行分组的目的
				let userGroupList = {}
				userSort.map((user,index)=>{
					if(!userGroupList[user.tag]){
						userGroupList[user.tag] = {}
						userGroupList[user.tag].top = index * 50
						userGroupList[user.tag].users = []
					}
					userGroupList[user.tag].users.push(user)
				})
				console.log(userGroupList)
				return userGroupList
			},
			groupTags(){
				// 第一步：我们要获取整个 的内容里面的 tag 所有
				let arr = userList.map(item => item.tag)
				// 第二步：数组去重
				// console.log(Array.from(new Set(arr)))
				// 第三部：进行拍讯
				// console.log(Array.from(new Set(arr)).sort())
				return Array.from(new Set(arr)).sort()
			}
		},
		methods:{
			gotoTag(item){
				this.currentTag = item
				uni.pageScrollTo({
					duration:500,
					scrollTop: this.userGroupList[item].top
				})
			},
			gotoUserInfo(user){
				console.log(user)
				uni.navigateTo({
					url:'/user/userinfo?id='+user
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user{
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10px 25upx;
		border-bottom: 1upx solid #f1f1f1;
		.avatar{
			width: 30px;
			height: 30px;
			border-radius: 6upx;
		}
		.name{
			margin-left: 20upx;
			color: #666;
		}
	}
	.grouo-indexs{
		position: fixed;
		z-index: 30;
		right: 0;
		  top: 50%;
		  transform: translateY(-50%);
		  width: 20px;
		  border-radius: 10px;
		  text-align: center;
		  background: rgba(0, 0, 0, 0.3);
		  font-family: Helvetica;
		  padding: 20rpx 0;
		  margin-right: 20rpx;
		.gindex {
		  color: #fff;
		  font-size: 24rpx;
		  padding: 6rpx 0;
		}
		.chosed{
			color: red;
		}
	}
</style>
